package com.luanye.enjoy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luanye.enjoy.constants.CommentConst;
import com.luanye.enjoy.entity.Comment;
import com.luanye.enjoy.entity.User;
import com.luanye.enjoy.enums.ResultCode;
import com.luanye.enjoy.mapper.CommentMapper;
import com.luanye.enjoy.mapper.UserMapper;
import com.luanye.enjoy.service.CommentService;
import com.luanye.enjoy.service.UserService;
import com.luanye.enjoy.utils.BeanCopyUtils;
import com.luanye.enjoy.vo.CommentVo;
import com.luanye.enjoy.vo.PageVo;
import com.luanye.enjoy.vo.RespBean;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;


@Service("commentService")
@AllArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final UserService userService;

    private final CommentMapper commentMapper;

    private final UserMapper userMapper;

    @Override
    public RespBean commentList(String type, Long articleId, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(CommentConst.ARTICLE_COMMENT.equals(type),Comment::getArticleId,articleId);

        queryWrapper.eq(Comment::getRootId, CommentConst.COMMENT_ROOT_ID);

        queryWrapper.eq(Comment::getType,type);

        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo : commentVoList) {
            //查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        return RespBean.success(new PageVo(commentVoList,page.getTotal())) ;
    }

    private List<CommentVo> getChildren(Long id) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        //时间排序
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);

        List<CommentVo> commentVos = toCommentVoList(comments);
        return commentVos;
    }

    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //遍历vo集合
        for (CommentVo commentVo : commentVos) {
            //通过creatyBy查询用户的昵称并赋值
            User user = userService.getById(commentVo.getCreateBy());
            String nickName = user.getNickname();
            String avatar = user.getAvatar();
            commentVo.setNickname(nickName);
            commentVo.setAvatar(avatar);
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if(commentVo.getToCommentUserId()!=-1){
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickname();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVos;
    }

    @Override
    public RespBean addComment(Comment comment, String openid) {
        if (!StringUtils.hasText(comment.getContent())){
            return RespBean.error(ResultCode.COMMENT_NOT_NULL);
        }
        User userByOpenId = userMapper.getUserByOpenId(openid);
        Long id = userByOpenId.getId();
        comment.setUpdateBy(id);
        comment.setCreateBy(id);
        save(comment);
        return RespBean.success();
    }

    @Override
    public RespBean getNum(Long articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,articleId);
        Long count = commentMapper.selectCount(queryWrapper);
        return RespBean.success(count);
    }
}
